package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String pwd) {
        pwd = MD5Util.md5(pwd);
        User user = userMapper.login(username);
        if (user != null) {
            if (pwd.equals(user.getPwd())) {
                return user;
            }
        }
        return null;
    }

    @Override
    public Boolean add(String username, String pwd, Integer age) {
        String pp = MD5Util.md5(pwd);
        User user = new User();
        user.setUsername(username);
        user.setPwd(pp);
        user.setAge(age);
        int t = userMapper.add(user);
        if (t > 0) return true;
        return false;
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAll();
    }

    @Override
    public String deleteUserByName(String username) {
        int ii = userMapper.deleteUserByName(username);
        if (ii > 0)
            return "删除成功";
        return "删除失败";
    }

    @Override
    public String modifyUserByName(String username, String pwd, Integer age) {
        pwd = MD5Util.md5(pwd);
        User user = new User();
        user.setUsername(username);
        user.setPwd(pwd);
        user.setAge(age);
        int oo = userMapper.modifyUserByName(user);
        if (oo > 0)
            return "修改成功";
        return "修改失败";
    }


}
